package wblog.domain;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class User {

    public String id;
    public byte[] pwd;
    public String name;
    public String tags;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public byte[] getPwd() {
        return pwd;
    }

    public void setPwd(byte[] pwd) {
        this.pwd = pwd;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTags() {
        return tags;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }

    public Map<String, Integer> getTagList() {
        if (tags == null || (tags = tags.trim()).isEmpty()) {
            return Collections.emptyMap();
        }
        String[] ss = tags.split("\\s+");
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (int i = 1; i < ss.length; i += 2) {
            map.put(ss[i], Integer.valueOf(ss[i + 1]));
        }
        return map;
    }

    public int getArticleCount() {
        if (tags == null || (tags = tags.trim()).isEmpty()) {
            return 0;
        }
        return Integer.parseInt(tags.split("\\s+")[0]);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (!id.equals(user.id)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }
}
